﻿package birdry.ui		{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import birdry.utils.ArrayUtil;
	
	/**
	* ...	드래그를 체크하여 드래그 수치만큼 목표수치를 감속시킨다.
	* @author DefaultUser (Tools -> Custom Arguments...)
	*/
	
	public class  DragRetardation		{
		
		public function DragRetardation(): void	{
		}
		
		public static function dragStart(stage:Stage, object:Object, value:Object, speed:Number = .2,  amount:Number = 1, reverse:Boolean = true): void	{
			
			var _value:Object = value;
			var _object:Object = object;
			var _stage:Stage = stage;
			var _speed:Number = speed;
			
			var _saveMouseX:Number = 0;
			var _saveMouseY:Number = 0;
			var _targetX:Number = 0;
			var _targetY:Number = 0;
			var _amount:Number = 1;
			var _reverse:Boolean = reverse;
			var _downCheck:Boolean;
			
			_stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			
			function onMouseDown( e: MouseEvent ): void	{
				_downCheck = true;
				_saveMouseX = _stage.mouseX;
				_saveMouseY = _stage.mouseY;
				_stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
				_stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
				_stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
			
			var deltaX:Number;
			var deltaY:Number;
			function onEnterFrame( e: Event ): void	{
				deltaX = (_targetX - _object[_value.x]);
				deltaY = (_targetY - _object[_value.y]);
				_object[_value.x] += _speed * deltaX;
				_object[_value.y] += _speed * deltaY;
				if (Math.abs(deltaX) < 0.01 && Math.abs(deltaY) < 0.01)	{
					_object[_value.x] = _targetX;
					_object[_value.y] = _targetY;
					if(!_downCheck)	_stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
				}
			}
			
			function onMouseMove( e: MouseEvent ): void	{
				if (_reverse)	{
					_targetX = _object[_value.x] + (_stage.mouseX - _saveMouseX) * _amount;
					_targetY = _object[_value.y] + (_stage.mouseY - _saveMouseY) * _amount;
				}	else 	{
					_targetX = _object[_value.x] + (_saveMouseX - _stage.mouseX) * _amount;
					_targetY = _object[_value.y] + (_saveMouseY - _stage.mouseY) * _amount;
				}
				_saveMouseX = _stage.mouseX;
				_saveMouseY = _stage.mouseY;
			}
			
			function onMouseUp( e: MouseEvent ): void	{
				_downCheck = false;
				_stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
				_stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			}
		}
		
	}	// class
	
}	// package